name: Test Store

# see: https://help.github.com/en/actions/reference/events-that-trigger-workflows
on:
  push:
    branches: [master, "release/*"]
  pull_request:
    branches: [master, "release/*"]
    types: [opened, reopened, ready_for_review, synchronize] # added `ready_for_review` since draft is skipped
    paths:
      - ".actions/*"
      - "requirements/ci.txt"
      - "requirements/store/**"
      - "src/lightning/__init__.py"
      - "src/lightning/__setup__.py"
      - "src/lightning/__version__.py"
      - "src/lightning/store/**"
      - "tests/tests_store/**"
      - "pyproject.toml" # includes pytest config
      - ".github/workflows/ci-tests-store.yml"
      - "!requirements/*/docs.txt"
      - "!*.md"
      - "!**/*.md"

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}-${{ github.head_ref }}
  cancel-in-progress: ${{ github.event_name == 'pull_request' }}

defaults:
  run:
    shell: bash

jobs:
  store-cpu:
    runs-on: ${{ matrix.os }}
    if: github.event.pull_request.draft == false
    strategy:
      fail-fast: false
      matrix:
        os: ["macOS-11", "ubuntu-20.04", "windows-2022"]
        pkg-name: ["lightning"]
        python-version: ["3.10"]
        pytorch-version: ["2.0"]
    timeout-minutes: 25 # because of building grpcio on Mac
    env:
      PACKAGE_NAME: ${{ matrix.pkg-name }}
      FREEZE_REQUIREMENTS: ${{ ! (github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/heads/release/')) }}
      # PYPI_CACHE_DIR: "_pip-wheels"
      TORCH_URL_STABLE: "https://download.pytorch.org/whl/cpu/torch_stable.html"
    steps:
      - uses: actions/checkout@v4

      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ matrix.python-version }}

      - name: Adjust PyTorch versions in requirements files
        if: ${{ matrix.requires != 'oldest' && matrix.release != 'pre' }}
        run: |
          pip install -q -r requirements/ci.txt
          python -m wget https://raw.githubusercontent.com/Lightning-AI/utilities/main/scripts/adjust-torch-versions.py
          for fpath in `ls requirements/store/*.txt`; do \
            python ./adjust-torch-versions.py $fpath ${{ matrix.pytorch-version }}; \
          done

      - name: Install package & dependencies
        timeout-minutes: 20
        run: |
          pip install -e ".[store,store-test]" -U -f ${TORCH_URL} --prefer-binary
          pip list

      - name: Testing Store
        working-directory: tests/tests_store
        # NOTE: do not include coverage report here, see: https://github.com/nedbat/coveragepy/issues/1003
        run: |
          python -m coverage run --source lightning \
            -m pytest -v --timeout=60 --durations=60

      - name: Statistics
        if: success()
        working-directory: tests/tests_store
        run: |
          coverage report
          coverage xml

      - name: Upload coverage to Codecov
        uses: codecov/codecov-action@v4
        # see: https://github.com/actions/toolkit/issues/399
        continue-on-error: true
        with:
          token: ${{ secrets.CODECOV_TOKEN }}
          file: tests/tests_store/coverage.xml
          flags: lightning,cpu,pytest,python${{ matrix.python-version }}
          name: CPU-coverage
          fail_ci_if_error: false
